<title>权限模块</title>

<div class="layui-card layadmin-header">
    <div class="layui-breadcrumb" lay-filter="breadcrumb">
        <a><cite>系统管理</cite></a>
        <a><cite>权限模块</cite></a>
    </div>
</div>

<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-form layui-card-header layuiadmin-card-header-auto" lay-filter="layadmin-permission-formlist">
            <div class="layui-form-item">
                <div class="layui-inline">
                    <div class="layui-input-inline">
                        <input type="text" name="name" placeholder="模块名称" autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-inline">
                    <button class="layui-btn layuiadmin-btn-permission" lay-submit
                            lay-filter="LAY-sys-permission-search">
                        搜索
                    </button>
                    <button class="layui-btn layuiadmin-btn-permission" data-type="add">
                        添加
                    </button>
                </div>
            </div>
        </div>

        <div class="layui-card-body">
            <table id="LAY-sys-module-table" lay-filter="LAY-sys-module-table"></table>
            <script type="text/html" id="table-module-admin">
                <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="edit">
                    <i class="layui-icon layui-icon-edit"></i>编辑</a>
                <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del"><i
                        class="layui-icon layui-icon-delete"></i>删除</a>

            </script>
        </div>
    </div>
</div>

<script type="text/html" id="modelModule">
    <form id="modelModuleForm" lay-filter="modelModuleForm" class="layui-form model-form">
        <div class="layui-form-item">
            <label class="layui-form-label">模块名称</label>
            <div class="layui-input-block">
                <input name="name" placeholder="请输入模块名称" type="text" class="layui-input" maxlength="50"
                       lay-verType="tips" lay-verify="required" required/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">模块描述</label>
            <div class="layui-input-block">
                <input name="desc" placeholder="请输入模块描述" type="text" class="layui-input" maxlength="50"
                       lay-verType="tips" lay-verify="required" required/>
            </div>
        </div>
        <div class="layui-form-item text-right">
            <button class="layui-btn layui-btn-fluid" lay-filter="modelModuleFormSubmit" lay-submit>保存</button>
        </div>
    </form>
</script>

<script>
    layui.use(['admin', 'table', 'setter'], function () {
        var $ = layui.$
            // , setter = layui.setter
            , admin = layui.admin
            , view = layui.view
            , table = layui.table
            , form = layui.form
            , setter = layui.setter;

        var apiUrl = setter.apiUrl;

        admin.req({
            url: apiUrl + '/mapi/permission/pm/drop_list'
            , method: 'get'
            , done: function (response) {
                if (response.code === 0) {
                    var list = response.data;
                    var html = "";
                    for (var i = 0; i < list.length; i++) {
                        html += "<option value=" + list[i]["id"] + ">" + list[i]["name"] + "</option>";
                    }
                    $("#topMenu").append(html);
                    form.render('select');
                }
            }
        });


        //管理员管理
        table.render({
            elem: '#LAY-sys-module-table'
            , url: apiUrl + '/mapi/permission/pm/list'
            , method: 'post'
            , page: true
            , contentType: 'application/json'
            , headers: {'token': layui.data(setter.tableName).token}
            , request: {
                pageName: 'pageNo'
                , limitName: 'pageSize'
            }
            , response: {
                statusCode: 0, //规定数据状态的字段名称，默认：code
            }
            , parseData: function (res) {
                if (res.code === 0) {
                    //将原始数据解析成 table 组件所规定的数据
                    return {
                        "code": res.code, //解析接口状态
                        "msg": res.msg, //解析提示文本
                        "count": res.data.total, //解析数据长度
                        "data": res.data.list //解析数据列表
                    };
                } else {
                    return {
                        "code": res.code, //解析接口状态
                        "msg": res.msg, //解析提示文本
                    };
                }
            }
            , cols: [[
                // {type: 'checkbox', fixed: 'left'}
                {field: 'id', width: 60, title: 'id'}
                , {field: 'name', title: '模块名称'}
                , {field: 'desc', title: '模块描述'}
                , {title: '操作', fixed: 'right', toolbar: '#table-module-admin'}
            ]]
            , text: {
                none: '未检索到数据' //默认：无数据。注：该属性为 layui 2.2.5 开始新增
            }
        });


        //监听搜索
        form.on('submit(LAY-sys-permission-search)', function (data) {
            var field = data.field;
            //执行重载
            table.reload('LAY-sys-module-table', {
                where: field
            });
        });

        // 工具条点击事件
        table.on('tool(LAY-sys-module-table)', function (obj) {
            var data = obj.data;
            var layEvent = obj.event;
            if (layEvent === 'edit') { // 修改
                showEditModel(data);
            } else if (layEvent === 'del') { // 添加
                top.layer.confirm('确定要删除' + data.name + '吗？', {
                        skin: 'layui-layer-admin'
                    }, function (index) {
                        top.layer.close(index);
                        admin.req({
                            url: apiUrl + '/mapi/permission/pm/delete/' + data.id
                            , method: 'delete'
                            , done: function (response) {
                                if (response.code === 0) {
                                    layer.msg('删除成功', {
                                        offset: '15px', icon: 1, time: 1000
                                    }, function () {
                                        layui.table.reload('LAY-sys-module-table'); //重载表格
                                    });
                                } else {
                                    layer.msg(response.msg, {
                                        offset: '15px', icon: 2
                                    });
                                }
                            }
                        });
                    }
                );
            }
        });

        function showEditModel(curData) {
            admin.popup({
                title: '编辑模块'
                // , area: ['420px', '430px']
                , id: 'LAY-popup-permission-add'
                , content: $('#modelModule').html()
                , success: function (layero, index) {
                    form.val('modelModuleForm', curData);  // 回显数据
                    // 表单提交事件
                    form.on('submit(modelModuleFormSubmit)', function (data) {
                        //提交 Ajax 成功后，关闭当前弹层并重载表格
                        admin.req({
                            url: apiUrl + '/mapi/permission/pm/update/' + curData.id
                            , method: 'put'
                            , data: JSON.stringify(data.field)
                            , done: function (response) {
                                if (response.code === 0) {
                                    layer.msg('保存成功', {
                                        offset: '15px', icon: 1, time: 1000
                                    }, function () {
                                        layer.close(index); //执行关闭
                                        layui.table.reload('LAY-sys-module-table'); //重载表格
                                    });
                                } else {
                                    layer.msg(response.msg, {
                                        offset: '15px', icon: 2
                                    });
                                }
                            }
                        });
                        return false;
                    });
                }
            });
        }

        //事件
        var active = {
            add: function () {
                var checkStatus = table.checkStatus('LAY-sys-module-table'), curData = checkStatus.data; //得到选中的数据
                admin.popup({
                    title: '添加模块'
                    // , area: ['420px', '430px']
                    , id: 'LAY-popup-permission-add'
                    , content: $('#modelModule').html()
                    , success: function (layero, index) {
                        // 表单提交事件
                        form.on('submit(modelModuleFormSubmit)', function (data) {
                            //提交 Ajax 成功后，关闭当前弹层并重载表格
                            admin.req({
                                url: apiUrl + '/mapi/permission/pm/add'
                                , method: 'post'
                                , data: JSON.stringify(data.field)
                                , done: function (response) {
                                    if (response.code === 0) {
                                        layer.msg('保存成功', {
                                            offset: '15px', icon: 1, time: 1000
                                        }, function () {
                                            layer.close(index); //执行关闭
                                            layui.table.reload('LAY-sys-module-table'); //重载表格
                                        });
                                    } else {
                                        layer.msg(response.msg, {
                                            offset: '15px', icon: 2
                                        });
                                    }
                                }
                            });
                            return false;
                        });
                    }
                });
            }
        }


        $('.layui-btn.layuiadmin-btn-permission').on('click', function () {
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });

    });
</script>